if (SERVER) then
	AddCSLuaFile("shared.lua");
end;

if (CLIENT) then
	SWEP.PrintName			= "Hands";
	SWEP.Instructions		 = "Left click to hit.";

	SWEP.Slot			= 1;
	SWEP.SlotPos		= 1;
	SWEP.ViewModelFlip 	= false;
	SWEP.DrawAmmo 		= false;
	SWEP.ViewModelFOV	= 62;
end;

SWEP.Base = "lgtrp_weapon_base";

SWEP.AdminSpawnable		= true;

SWEP.WorldModel 		= Model("models/weapons/w_fists.mdl");
SWEP.ViewModel			= Model("models/weapons/v_fists.mdl");

SWEP.Primary.ClipSize		= 1;
SWEP.Primary.DefaultClip	= 1;
SWEP.Primary.Automatic		= false;
SWEP.Primary.Ammo			= "none";

SWEP.Secondary.ClipSize		= 1;
SWEP.Secondary.DefaultClip	= 1;
SWEP.Secondary.Automatic	= false;
SWEP.Secondary.Ammo			= "none";

SWEP.HolsteredPos 		= Vector(-2.4, -4, -12);
SWEP.HolsteredAngle		= Vector(0, 0, 0);

function SWEP:Initialize()
	self.HoldType = "fist";
	self.holsterType = "normal";
end;

function SWEP:Reload()
end;

function SWEP:CustomThink()
	if (SERVER) then
		if (self.Owner.Untying) then
			if (!self.Owner:KeyDown(IN_ATTACK2)) then
				self.Owner.Untying = false;
				self.Owner.UntieTarget:SetNWBool("lgtrp_btied", false);
				self.Owner:SetNWBool("lgtrp_tying", false);
				
				umsg.Start("lgtrp_runtie", self.Owner)
				umsg.End();
			else
				local tr = self.Owner:EyeTrace(99999999);
				local entity;
				
				if (IsValid(tr.Entity)) then
					if (tr.Entity:IsRagdoll()) then
						entity = self.Owner.UntieTarget;
					else
						entity = tr.Entity;
					end;
				end;
				
				if (tr.HitWorld or IsValid(entity) and entity != self.Owner.UntieTarget or self.Owner:GetPos():Distance(entity:GetPos()) > 120) then
					self.Owner.Untying = false;
					self.Owner.UntieTarget:SetNWBool("lgtrp_btied", false);
					self.Owner:SetNWBool("lgtrp_tying", false);
					
					umsg.Start("lgtrp_runtie", self.Owner)
					umsg.End();
				end;
				
				if (self.Owner.UntieTime +5 < CurTime()) then
					self.Owner.Untying = false;
					self.Owner.UntieTarget:SetNWBool("lgtrp_tied", false);
					self.Owner.UntieTarget:SetNWBool("lgtrp_btied", false);
					self.Owner:SetNWBool("lgtrp_tying", false);
					
					self.Owner.UntieTarget = nil;
					
					umsg.Start("lgtrp_runtie", self.Owner)
					umsg.End();
				end;
			end;
		end;
	end;
end;

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() +0.5);

	if (!self.Owner:GetNWBool("lgtrp_holstered") or self.Owner:IsTied()) then return; end;
	
	-- Do some animations.
	self.Owner:SetAnimation(PLAYER_ATTACK1);
	self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER);
	
	--shoot bullet
	
	-- Emit a sound.
	if (SERVER) then
		self.Owner:EmitSound("npc/vort/claw_swing" .. math.random(1, 2) .. ".wav");
	
		local tr = self.Owner:EyeTrace(90);
	
		if (tr.Entity:IsValid()) then
			-- incerase strength
			
			local entity = tr.Entity;
		
			if (entity:IsPlayer()) then
				self.Owner:EmitSound("physics/flesh/flesh_impact_hard" .. math.random(1, 6) .. ".wav");
			
				local bloodEffect = EffectData();
					bloodEffect:SetOrigin(tr.HitPos);
				util.Effect("BloodImpact", bloodEffect, true, true);

				if (entity:Health() < 25 and entity:Health() > 2) then
					entity:SpawnRagdoll();
					
					timer.Simple(26, function()
						if (IsValid(entity)) then
							entity:RemoveRagdoll(true, true);
							
							umsg.Start("lgtrp_unconscious", entity);
								umsg.Bool(false);
							umsg.End();
						end;
					end, entity);
				end;
				
			--	entity:TakeDamage(math.random(5, 10), self.Owner, self.Weapon);
			elseif (tr.Hit) then
				self.Owner:EmitSound("physics/flesh/flesh_impact_hard" .. math.random(1, 5) .. ".wav");
			end;
		elseif (tr.Hit) then
			self.Owner:EmitSound("physics/flesh/flesh_impact_hard" .. math.random(1, 5) .. ".wav");
		end;
	end;
end;

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire(CurTime() +0.2);
	if (self.Owner:GetNWBool("lgtrp_holstered") or self.Owner:IsTied()) then return; end;
	
	if (SERVER) then
		local tr = self.Owner:EyeTrace(90);
		
		if (IsValid(tr.Entity)) then
			if (tr.Entity:IsDoor()) then
				self.Owner:EmitSound("physics/wood/wood_crate_impact_hard2.wav");
			else
				local valid;
			
				if (tr.Entity:IsRagdoll() and tr.Entity:GetRagdollPlayer()) then
					valid = tr.Entity:GetRagdollPlayer();
				elseif (tr.Entity:IsPlayer()) then
					valid = tr.Entity;
				end;
			
				if (IsValid(valid) and valid:IsTied()) then
					self.Owner.Untying = true;
					self.Owner.UntieTime = CurTime();
					self.Owner.UntieTarget = valid;
					
					self.Owner:SetNWBool("lgtrp_tying", true);
					valid:SetNWBool("lgtrp_btied", true);
					
					umsg.Start("lgtrp_untie", self.Owner)
					umsg.End();
				end;
			end;
		end;
	end;
end;